package controller;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import view.ContactProfile;
import view.MainWindow;
import model.Contact;
import model.ContactListModel;

/**
 * This controller redirect to a model to display a contact profile when the user clic on a name in the list of contacts
 * @author Marie Morin
 *
 */
public class SelectController implements ListSelectionListener{
	
	private MainWindow w;
	ArrayList<Contact> contactsList = new ArrayList<Contact>();
	
	
	public SelectController(MainWindow w){
		
		this.w = w ;
		
		ContactListModel c = new ContactListModel();	
		contactsList = c.getContactsList();


		
	}

	public ArrayList<Contact> getContactsList() {
		return contactsList;
	}

	public void setContactsList(ArrayList<Contact> contactsList) {
		this.contactsList = contactsList;
	}
	
	/**
	 *  This function is called when the user click on the name of a contact in the list, to display his/her profile
	 */
	@Override
	 public void valueChanged(ListSelectionEvent e) {
       if (!e.getValueIsAdjusting()){
    	   
           JList source = (JList)e.getSource();
           String sContact = source.getSelectedValue().toString();

          	
    		for (int i = 0; i< contactsList.size(); i++){
    			
    			// Compare each contact of the list with the selected contact
    			if (contactsList.get(i).getAname().compareTo(sContact)==0){

   			
    				
    				/* Declaration of a thread to modify the interface by :
    				 * - displaying the profile of the contact selected 
    				 * - updating the contact list
    				*/
    				
    				try {
						ContactProfile contact = new ContactProfile(contactsList.get(i), w);
					} catch (URISyntaxException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}


					
					 
					 
    			}
	
    		}
    		
    		

       }
   }

}
